package com.example.alee.ALeeSpringWeb.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

@Service
public class ReadConfig {

    //注入环境对象
    private final Environment environment;

    public ReadConfig(Environment environment) {
        this.environment = environment;
    }

    public void print() {

        String name = environment.getProperty("app.name");

        if (environment.containsProperty("app.owner")) {
            System.out.println("owner 存在");
        } else {
            System.out.println("owner 不存在");
        }

        //没有数值，提供默认值
        int port = environment.getProperty("app.port", Integer.class, 9001);

        String str = String.format("读取的数值为 name=%s ， port=%d", name, port);
        System.out.println(str);
    }
}
